package com.tianyang.yukuaifu.rxservice.client;

import android.text.TextUtils;

import androidx.core.util.Pair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tianyang.yukuaifu.business.trade.bean.AgentWalletOutput;
import com.tianyang.yukuaifu.business.trade.bean.EarningsTypeOutput;
import com.tianyang.yukuaifu.business.trade.bean.WithdrawOutput;
import com.tianyang.yukuaifu.business.trade.bean.WithdrawRecordOutput;
import com.tianyang.yukuaifu.common.interfaces.UrlConstants;
import com.tianyang.yukuaifu.common.utils.CommonViewUtils;
import com.tianyang.yukuaifu.rxservice.BaseClient;
import com.tianyang.yukuaifu.rxservice.MetaBean;
import com.tianyang.yukuaifu.rxservice.interfaces.PlateTradeService;

import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.functions.Func1;

/**
 * Description：plate-trade接口
 * Created by xyj on 2024/3/18
 */
public class PlateTradeClient extends BaseClient {

    private static PlateTradeClient mInstance;

    public static PlateTradeClient getInstance() {
        if (mInstance == null) {
            synchronized (PlateTradeClient.class) {
                if (mInstance == null) {
                    mInstance = new PlateTradeClient();
                }
            }
        }
        return mInstance;
    }

    private PlateTradeService service;

    private PlateTradeClient() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstants.BASE_TRADE_URL)
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(PlateTradeService.class);
    }

    /**
     * 商户延迟合并代付订单详情
     * @param reqData 请求参数
     */
    public Observable<JSONObject> merchantDelayTradeDetail(Map<String, String> reqData) {
        return callMap(service.merchantDelayTradeDetail(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 账户余额
     * @param reqData 请求参数
     */
    public Observable<AgentWalletOutput> getAgentWallet(Map<String, String> reqData) {
        return callMap(service.getAgentWallet(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<AgentWalletOutput>>() {
                    @Override
                    public Observable<AgentWalletOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        AgentWalletOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), AgentWalletOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 收益类别汇总
     * @param reqData 请求参数
     */
    public Observable<List<EarningsTypeOutput>> getSumUserWallet(Map<String, String> reqData) {
        return callMapList(service.getSumUserWallet(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<EarningsTypeOutput>>>() {
                    @Override
                    public Observable<List<EarningsTypeOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<EarningsTypeOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<EarningsTypeOutput>>(){});
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 收益明细
     */
    public Observable<JSONObject> getUserWallet(String params) {
        return callMap(service.getUserWallet(UrlConstants.getUserWallet + params, CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 提现记录
     * @param reqData 请求参数
     */
    public Observable<List<WithdrawRecordOutput>> gettixianRecord(Map<String, Object> reqData) {
        return callMapList(service.gettixianRecord(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<WithdrawRecordOutput>>>() {
                    @Override
                    public Observable<List<WithdrawRecordOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<WithdrawRecordOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<WithdrawRecordOutput>>(){});
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 服务商提现接口
     * @param reqData 请求参数
     */
    public Observable<WithdrawOutput> getcantixian(Map<String, Object> reqData) {
        return callMap(service.getcantixian(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<WithdrawOutput>>() {
                    @Override
                    public Observable<WithdrawOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        WithdrawOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<WithdrawOutput>(){});
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 提交提现请求
     * @param reqData 请求参数
     */
    public Observable<JSONObject> postixian(Map<String, Object> reqData) {
        return callMap(service.postixian(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 设置结算周期
     * @param params 请求参数
     */
    public Observable<JSONObject> updateMemberSettleCycle(String params) {
        return callMap(service.updateMemberSettleCycle(UrlConstants.updateMemberSettleCycle + params, CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }
}
